package connection;

import javax.persistence.EntityManager;

public class EMFLocal {
	private static ThreadLocal<EntityManager> emLocal = new ThreadLocal<EntityManager>();

	public static synchronized EntityManager getEntityManager() {
		EntityManager em = emLocal.get();
		if (em == null) {
			em = EMF.get().createEntityManager();
			emLocal.set(em);
		}
		return em;
	}

	public static void limparEntityManager() {
		EntityManager em = emLocal.get();
		if (em != null) {
			emLocal.get().close();
			emLocal.set(null);
		}
	}

}
